package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.common.ConsoleConstant;
import com.fdb.efp.nls.service.dao.PrdCaseManageDao;
import com.fdb.efp.nls.service.domain.PrdCaseManage;
import com.fdb.efp.nls.service.facade.PrdCaseManageService;
import com.fdb.efp.nls.service.facade.PubSysInfoService;
import com.fdb.efp.nls.service.vo.PrdCaseManageVO;
import com.fdb.efp.flow.common.vo.PageApproveVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 产品专案管理表				<p>
 * @author fuzm
 * @date 2018-05-07
 */
@Service("prdCaseManageService")
public class PrdCaseManageServiceImpl implements PrdCaseManageService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(PrdCaseManageServiceImpl.class);

	@Autowired
	private PrdCaseManageDao prdCaseManageDao;
	@Autowired
	private PubSysInfoService pubSysInfoService ;
	
	/**
	 * 新增操作
	 */
	public int insertPrdCaseManage(PrdCaseManageVO inPrdCaseManageVo) {
		logger.debug("当前新增数据为:"+ inPrdCaseManageVo.toString());
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			
//			setInsertDefaultProperty(inPrdCaseManageVo);
			beanCopy(inPrdCaseManageVo, prdCaseManage);
			num = prdCaseManageDao.insertPrdCaseManage(prdCaseManage);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(PrdCaseManageVO inPrdCaseManageVo) {
		logger.debug("当前删除数据条件为:"+ inPrdCaseManageVo);
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			beanCopy(inPrdCaseManageVo, prdCaseManage);
			num = prdCaseManageDao.deleteByPk(prdCaseManage);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPrdCaseManageVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(PrdCaseManageVO inPrdCaseManageVo) {
		logger.debug("当前修改数据为:"+ inPrdCaseManageVo.toString());
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			setUpdateDefaultProperty(inPrdCaseManageVo);
			beanCopy(inPrdCaseManageVo, prdCaseManage);
			num = prdCaseManageDao.updateByPk(prdCaseManage);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPrdCaseManageVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public PrdCaseManageVO queryByPk(PrdCaseManageVO inPrdCaseManageVo) {
		
		logger.debug("当前查询参数信息为:"+ inPrdCaseManageVo);
		try {
			PrdCaseManage queryPrdCaseManage = new PrdCaseManage();
			beanCopy(inPrdCaseManageVo, queryPrdCaseManage);
			PrdCaseManage queryRslPrdCaseManage = prdCaseManageDao.queryByPk(queryPrdCaseManage);
			if (Objects.nonNull(queryRslPrdCaseManage)) {
				PrdCaseManageVO outPrdCaseManageVo = beanCopy(queryRslPrdCaseManage, new PrdCaseManageVO());
				logger.debug("当前查询结果为:"+ outPrdCaseManageVo.toString());
				return outPrdCaseManageVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PrdCaseManageVO> queryAllOwner(PrdCaseManageVO prdCaseManageVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PrdCaseManageVO> list = null;
		try {
			List<PrdCaseManage> prdCaseManages = prdCaseManageDao.queryAllOwnerByPage(prdCaseManageVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ prdCaseManages.size());
			pageSet(prdCaseManages, prdCaseManageVo);
			list = (List<PrdCaseManageVO>) beansCopy(prdCaseManages, PrdCaseManageVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PrdCaseManageVO> queryAllCurrOrg(PrdCaseManageVO prdCaseManageVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<PrdCaseManage> prdCaseManages = prdCaseManageDao.queryAllCurrOrgByPage(prdCaseManageVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+prdCaseManages.size());
		List<PrdCaseManageVO> list = null;
		try {
			pageSet(prdCaseManages, prdCaseManageVo);
			list = (List<PrdCaseManageVO>) beansCopy(prdCaseManages, PrdCaseManageVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PrdCaseManageVO> queryAllCurrDownOrg(PrdCaseManageVO prdCaseManageVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<PrdCaseManage> prdCaseManages = prdCaseManageDao.queryAllCurrDownOrgByPage(prdCaseManageVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ prdCaseManages.size());
		List<PrdCaseManageVO> list = null;
		try {
			pageSet(prdCaseManages, prdCaseManageVo);
			list = (List<PrdCaseManageVO>) beansCopy(prdCaseManages, PrdCaseManageVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	/**
	 * 根据产品编号更新专案状态
	 */
	public int updateCaseStatusByPrdId(PrdCaseManageVO inPrdCaseManageVo) {
		logger.debug("当前修改数据为:"+ inPrdCaseManageVo.toString());
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			setUpdateDefaultProperty(inPrdCaseManageVo);
			beanCopy(inPrdCaseManageVo, prdCaseManage);
			num = prdCaseManageDao.updateCaseStatusByPrdId(prdCaseManage);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPrdCaseManageVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 申请通过
	 */
	public void pass(String bizSerno, PageApproveVO pageApproveVo) {
		logger.debug("审批通过的当前修改数据为:"+ bizSerno);
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			prdCaseManage.setCaseCode(bizSerno);
			prdCaseManage =  prdCaseManageDao.queryByPk(prdCaseManage);
			String expiryDate = prdCaseManage.getExpiryDate();//失效日期
			String openday = pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001);
			//判断如果失效日期<当前日期，则直接将专案状态更新为失效
			int result = TimeUtil.compareDates(expiryDate, openday);
			if(result < 0) {
				prdCaseManage.setCaseStatus(ConsoleConstant.CASE_STATUS_04);//已失效
			}
			prdCaseManage.setAprvStatus(ConsoleConstant.APRV_STATUS_03);//通过
			setUpdateDefaultProperty(prdCaseManage);
			num = prdCaseManageDao.updateByPk(prdCaseManage);
			logger.debug("根据条件:"+ bizSerno +"审批通过时修改的数据条数为"+ num);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
	}
	
	/**
	 * 审批不通过后处理
	 */
	public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
		logger.debug("当前修改数据为:"+ bizSerno);
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			prdCaseManage.setCaseCode(bizSerno);
			prdCaseManage =  prdCaseManageDao.queryByPk(prdCaseManage);
			prdCaseManage.setAprvStatus(ConsoleConstant.APRV_STATUS_04);//不通过
			prdCaseManage.setCaseStatus(ConsoleConstant.CASE_STATUS_04);//已失效
			setUpdateDefaultProperty(prdCaseManage);
			num = prdCaseManageDao.updateByPk(prdCaseManage);
			logger.debug("根据条件:"+ bizSerno +"审批通过时修改的数据条数为"+ num);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
	}

	@Override
	public void repulse(String bizSerno, PageApproveVO pageApproveVo) {

	}

	/**
	 * 查询同一产品同一时间段内的专案信息
	 */
	public List<PrdCaseManageVO> queryPrdCaseManageByPrdCodeTime(PrdCaseManageVO prdCaseManageVo) {

		List<PrdCaseManage> prdCaseManages = prdCaseManageDao.queryPrdCaseManageByPrdCodeTime(prdCaseManageVo);
		logger.debug("结果集数量为:"+ prdCaseManages.size());
		List<PrdCaseManageVO> list = null;
		try {
			list = (List<PrdCaseManageVO>) beansCopy(prdCaseManages, PrdCaseManageVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	/**
	 * 查询产品的有效专案信息
	 */
	public List<PrdCaseManageVO> queryPrdCaseManageByPrdCode(PrdCaseManageVO prdCaseManageVo) {
		List<PrdCaseManage> prdCaseManages = prdCaseManageDao.queryPrdCaseManageByPrdCode(prdCaseManageVo);
		logger.debug("结果集数量为:"+ prdCaseManages.size());
		List<PrdCaseManageVO> list = null;
		if(prdCaseManages!=null && prdCaseManages.size()>0) {
			try {
				list = (List<PrdCaseManageVO>) beansCopy(prdCaseManages, PrdCaseManageVO.class);
			} catch (Exception e) {
				logger.error("数据转换出现异常!", e);
			}
		}
		
		return list;
	
	}
	
	/**
	 * 批量生效专案
	 */
	public int effictivePrdCaseManages(String openday) {
		logger.debug("数据日期为:"+ openday);
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			prdCaseManage.setEffictiveDate(openday);
			prdCaseManage.setCaseStatus(ConsoleConstant.CASE_STATUS_02);//已生效
			logger.info("参数："+ prdCaseManage);
			num = prdCaseManageDao.effictivePrdCaseManages(prdCaseManage);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.info("根据条件:"+ openday +"专案生效数据条数为"+ num);
		return num;
	}
	
	/**
	 * 批量失效到期专案
	 */
	public int expiryPrdCaseManages(String openday) {
		logger.debug("数据日期为:"+ openday);
		int num = 0;
		try {
			PrdCaseManage prdCaseManage = new PrdCaseManage();
			prdCaseManage.setExpiryDate(openday);
			prdCaseManage.setCaseStatus(ConsoleConstant.CASE_STATUS_04);//已失效
			logger.info("参数："+ prdCaseManage);
			num = prdCaseManageDao.expiryPrdCaseManages(prdCaseManage);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.info("根据条件:"+ openday +"专案失效数据条数为"+ num);
		return num;
	}
}
